﻿using System;
using System.Collections.Generic;
using System.Linq;
using EzDesk.Apps.Data.ProjectModule;
using EzDesk.Apps.Interface.ProjectModule;
using EzDesk.Apps.Model.ProjectModule;
using EzDesk.EzAppApi.Utility;
using EzDesk.Utility.ServiceStatusInfo;

namespace EzDesk.Apps.ServiceImpl.ProjectModule
{
    /// <summary>
    /// 项目相关服务
    /// </summary>
    public partial class ProjectService : IProjectService
    {
        public StatusInfo<Task> CreateTask(Task task)
        {
            return StatusInfo.Wrap(
            () => EzDeskProjectContainer.GoAndSave(container =>
            {
                task.Id = Guid.NewGuid();
                container.TaskSet.AddObject(task);
                container.SaveChanges();
                return task;
            }));
        }


        public StatusInfo DeleteTask(Guid taskId)
        {
            return StatusInfo.Wrap(
            () => EzDeskProjectContainer.GoAndSave(db =>
            {
                Task task = db.TaskSet.First(t => t.Id == taskId);
                foreach (var workitem in db.WorkitemSet.Where(wi => wi.TaskId == taskId))
                {
                    workitem.TaskId = null;
                }
                db.TaskSet.DeleteObject(task);
                db.SaveChanges();
            }));
        }
        public StatusInfo UpdateTask(Task newTask)
        {
            return StatusInfo.Wrap(
            () => EzDeskProjectContainer.GoAndSave(db =>
            {
                Task task = db.TaskSet.FirstOrDefault(t => t.Id == newTask.Id);

                sendTaskChangedUpdate(task, newTask);
                task.ChargePersonId = newTask.ChargePersonId;
                task.Duration = newTask.Duration;
                task.MasterTaskId = newTask.MasterTaskId;
                task.Name = newTask.Name;
                task.ParentTaskId = newTask.ParentTaskId;
                task.ProjectId = newTask.ProjectId;
                task.StartOn = newTask.StartOn;

                //    return db.WorkitemSet.Where(wi => wi.TaskId == taskId).ToList();
            }));
        }

        private void sendTaskChangedUpdate(Task orignalTask, Task newTask)
        {
            //throw new NotImplementedException("这里应该实现发送Feed更新的相关逻辑");

            //if(orignalWorkitem.OwnerId != newWorkitem.OwnerId)
            //if(orignalWorkitem.Points != newWorkitem.Points)
            //if(orignalWorkitem.Title != newWorkitem.Title)
            //if(orignalWorkitem.Description != newWorkitem.Description)
            //if(orignalWorkitem.PlanOn != newWorkitem.PlanOn)
        }


        public StatusInfo<List<Workitem>> GetTaskWorkitems(Guid taskId)
        {
            return StatusInfo.Wrap(
            () => EzDeskProjectContainer.GoAndSave(db =>
            {
                return db.WorkitemSet.Where(wi => wi.TaskId == taskId).ToList();

            }));
        }

        public StatusInfo<List<Task>> GetPorjectTasks(Guid projectId)
        {
            return StatusInfo.Wrap(() => GetFullProjectById(projectId).Result.RootTasks.ToList());
        }

        public StatusInfo<List<Task>> GetPorjectsTasks(List<Guid> projectIdList)
        {
            return StatusInfo.Wrap(
                        () => EzDeskProjectContainer
                            .GoAndSave(db => db.TaskSet
                                .Where(p => projectIdList.Contains(p.ProjectId))
                                .ToList()));
        }

    }

}